home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / Onboard / KeyGtk.py < prev    next >
Encoding:
Python Source  |  2008-03-03  |  5.0 KB  |  144 lines

  1. # -*- coding: UTF-8 -*-
  2.  
  3. import pango
  4. # from math import sqrt
  5. from KeyCommon import *
  6.  
  7. # BASE_PANE_TAB_HEIGHT = 40
  8.  
  9. class Key(KeyCommon):
  10.     def __init__(self, pane):
  11.         KeyCommon.__init__(self, pane)
  12.     def moveObject(self, x, y, context = None):
  13.         context.move_to(x, y)
  14.  
  15.     def createLayout(self, label):
  16.         self.layout = self.pane.keyboard.create_pango_layout(label)
  17.  
  18.     def paintFont(self, xScale, yScale, x, y, context = None):
  19.         KeyCommon.paintFont(self, xScale, yScale, x, y, context)
  20.  
  21.         if hasattr(self, "layout"):
  22.             context.set_source_rgb(0, 0, 0)
  23.             self.layout.set_font_description(pango.FontDescription("Sans Serif %d" %( self.fontScale * self.pane.fontSize)))
  24.             context.update_layout(self.layout)            
  25.             context.show_layout(self.layout)
  26.  
  27.  
  28. class TabKey(TabKeyCommon, Key):
  29.     def __init__(self, keyboard, width, pane):
  30.         TabKeyCommon.__init__(self, keyboard, width, pane)
  31.         Key.__init__(self, pane)
  32.  
  33.     def paint(self, context = None):
  34.         TabKeyCommon.paint(self, context)
  35.         context.rectangle(self.keyboard.kbwidth, 
  36.                           self.height * self.index + BASE_PANE_TAB_HEIGHT, self.width, self.height)
  37.  
  38.         if self.pane == self.keyboard.activePane and self.stuckOn:
  39.             context.set_source_rgba(1, 0, 0,1)
  40.         else:       
  41.             context.set_source_rgba(float(self.pane.rgba[0]), float(self.pane.rgba[1]),float(self.pane.rgba[2]),float(self.pane.rgba[3]))
  42.         
  43.         context.fill()
  44.  
  45.     
  46. class BaseTabKey(BaseTabKeyCommon, Key):
  47.     def __init__(self, keyboard, width):
  48.         BaseTabKeyCommon.__init__(self, keyboard, width)
  49.         Key.__init__(self, None)
  50.  
  51.     ''' this class has no UI-specific code at all. Why? '''
  52.     def paint(self,context):
  53.         #We don't paint anything here because we want it to look like the base pane.
  54.         pass
  55. class LineKey(LineKeyCommon, Key):
  56.     def __init__(self, pane, coordList, fontCoord, rgba):
  57.         LineKeyCommon.__init__(self, pane, coordList, fontCoord, rgba)
  58.         Key.__init__(self, pane)
  59.  
  60.     def pointWithinKey(self, widget, mouseX, mouseY):
  61.         """Cairo specific, hopefully fast way of doing this"""
  62.         context = widget.window.cairo_create()
  63.         self.draw_path(self.pane.xScale, self.pane.yScale, context)
  64.  
  65.         return context.in_fill(mouseX, mouseY)
  66.  
  67.     def paint(self, xScale, yScale, context):
  68.         self.draw_path(xScale, yScale, context)
  69.  
  70.         if (self.stuckOn):
  71.             context.set_source_rgba(1.0, 0.0, 0.0,1.0)
  72.         elif (self.on):
  73.             context.set_source_rgba(0.5, 0.5, 0.5,1.0)
  74.         elif (self.beingScanned):   
  75.             context.set_source_rgba(0.45,0.45,0.7,1.0)
  76.         else:
  77.             context.set_source_rgba(self.rgba[0], self.rgba[1],self.rgba[2],self.rgba[3])
  78.  
  79.         context.fill_preserve()
  80.         context.set_source_rgb(0, 0, 0)
  81.         context.stroke()
  82.  
  83.     def draw_path(self, xScale, yScale, context):
  84.         ''' currently this method contains all the LineKey 
  85.             painting code.¬†'''
  86.         LineKeyCommon.paint(self, xScale, yScale, context = None)
  87.         c = 2
  88.         context.move_to(self.coordList[0]*xScale, self.coordList[1]*yScale)
  89.         while not c == len(self.coordList):
  90.             xp1 = self.coordList[c+1]*xScale
  91.             yp1 = self.coordList[c+2]*yScale
  92.             try:
  93.                 if self.coordList[c] == "L":
  94.                     c +=3
  95.                     context.line_to(xp1,yp1)
  96.                 else:   
  97.                     xp2 = self.coordList[c+3]*xScale
  98.                     yp2 = self.coordList[c+4]*yScale
  99.                     xp3 = self.coordList[c+5]*xScale
  100.                     yp3 = self.coordList[c+6]*yScale
  101.                     context.curve_to(xp1,yp1,xp2,yp2,xp3,yp3)
  102.                     c += 7
  103.  
  104.             except TypeError, (strerror):
  105.                 print x
  106.                 print y
  107.                 print xp1
  108.                 print yp1
  109.                 print strerror
  110.  
  111.                 
  112.  
  113.     def paintFont(self, xScale, yScale, context = None):
  114.         Key.paintFont(self, xScale, yScale, 
  115.             self.fontCoord[0], self.fontCoord[1], context)
  116.  
  117.  
  118.     
  119. class RectKey(RectKeyCommon, Key):
  120.     def __init__(self, pane, x, y, width, height, rgba):
  121.         RectKeyCommon.__init__(self, pane, x, y, width, height, rgba)
  122.  
  123.     def paint(self, xScale, yScale, context = None):
  124.         
  125.         context.rectangle(self.x*xScale,self.y*yScale,self.width*xScale, self.height*yScale)
  126.         
  127.         if (self.stuckOn):
  128.             context.set_source_rgba(1, 0, 0,1)
  129.         elif (self.on):
  130.             context.set_source_rgba(0.5, 0.5, 0.5,1)
  131.         elif (self.beingScanned):   
  132.             context.set_source_rgba(0.45,0.45,0.7,1)
  133.         else:
  134.             context.set_source_rgba(self.rgba[0], self.rgba[1],self.rgba[2],self.rgba[3])
  135.         
  136.         context.fill_preserve()
  137.         context.set_source_rgb(0, 0, 0)
  138.         context.stroke()
  139.  
  140.     def paintFont(self, xScale, yScale, context = None):
  141.         Key.paintFont(self, xScale, yScale, self.x, self.y, context)
  142.  
  143.         
  144.